﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DeviceManagement;

namespace DeviceNotificationSample2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            InitializeDeviceNotifications();
        }

        private void InitializeDeviceNotifications()
        {
            var notificationHandler = new DeviceNotificationHandler();

            notificationHandler.DeviceArrival += new EventHandler<DeviceNotificationEventArgs>(notificationHandler_DeviceArrival);
			notificationHandler.DeviceQueryRemove += new EventHandler<DeviceNotificationEventArgs>(notificationHandler_DeviceQueryRemove);

            var deviceNotification = new DeviceNotification() { NotificationHandler = notificationHandler };

            deviceNotification.Register(DeviceType.Interface);

            this.components.Add(deviceNotification);
        }

		void notificationHandler_DeviceQueryRemove(object sender, DeviceNotificationEventArgs e)
		{
			DeviceNotificationCancelEventArgs cancelEventArgs = (DeviceNotificationCancelEventArgs) e;
			DialogResult result = MessageBox.Show("Do you want to allow removal of the device?", "Question", MessageBoxButtons.YesNo);
			if (result != DialogResult.Yes)
				cancelEventArgs.Cancel = true;

		}

        void notificationHandler_DeviceArrival(object sender, DeviceNotificationEventArgs e)
        {
            if (e.IsInterface())
            {
                DeviceInterface deviceInterface = e.GetInterface();

                Device dev = GetDevice(deviceInterface);
                if ((dev != null) && (!string.IsNullOrEmpty(dev.DisplayName)))
                    MessageBox.Show(string.Format("The following device has arrived: {0}", dev.DisplayName));

            }
        }

        static Device GetDevice(DeviceInterface deviceInterface)
        {
            using (DeviceInfoSet set = DeviceInfoSet.GetPresent(deviceInterface))
            {
                foreach (DeviceInfo device in set.GetDevices())
                {
                    if (set.HasInterface())
                    {
                        IEnumerable<DeviceInterfaceData> deviceInterfaceData = set.GetDeviceInterfaceData(device);
                        DeviceInterfaceData matchingInterfaceData;
                        if ((matchingInterfaceData = (deviceInterfaceData.Where((x) => CompareDevicePath(x.DevicePath, deviceInterface.Name))).SingleOrDefault()) != null)
                            return new Device() { InterfaceGuid = deviceInterface.ClassGuid, DevicePath = matchingInterfaceData.DevicePath, DisplayName = device.FriendlyName };

                    }
                }
            }

            return null;
        }

        static bool CompareDevicePath(string path1, string path2)
        {
            if (string.IsNullOrEmpty(path1) || (string.IsNullOrEmpty(path2)))
                return false;

            return path1.ToLower().Equals(path2.ToLower());
        }
    }

    class Device
    {
        public Guid InterfaceGuid { get; set; }

        public string DevicePath { get; set; }

        public string DisplayName { get; set; }
    }
}
